// pages/center/center.js
const app = getApp()
import {
  GetUserCRMInfo,
  daySign,
  gussYouLike,
  getWxQrCode,
  getInfoPicture
} from '../../api/apis.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    List: [
      // {
      //   text: "个人设置",
      //   url:"../setting/setting"
      // },
      // {
      //   text: "地址管理",
      //   url: "../receiving-address/receiving-address"
      // },
      {
        text: "手机绑定",
        url: "../bind-phone/bind-phone",
        noValue: false
      },
      {
        text: "关于我们",
        url: "", //"../login-way/login-way",
        noValue: true
      }
    ],
    logoList: [{
        text: "我的订单",
        url: "../order-list/order-list",
        icon: 'icon-icon-'
      },
      {
        text: "购物车",
        url: "../cart-tab/cart-tab",
        icon: 'icon-cart1'
      },
      {
        text: "手机绑定",
        url: "../bind-phone/bind-phone",
        icon: 'icon-phone1'
      },
      {
        text: "积分商城",
        url: "../integral-index/integral-index",
        icon: 'icon-shangcheng'
      },
      {
        text: "联系我们",
        isTab: true,
        url: "../message/message",
        icon: 'icon-kefu7'
      }
    ],
    Name: '', // 昵称
    Level: '', // VIP等级
    Integral: '', // 积分
    Img: '', // 头像
    Val: '', // 积分值 
    isVip: false,
    productList: [],
    pageIndex: 0,
    qrModa: false,
    qrCodeImg: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this._initData()
    this._getBgImg()
    this._getSalesIntegralProducts()
    this.setData({
      isVip: app.globalData.vipInfo.isVIP
    })
  },
  // onShow() {
  //   this._initData()
  //   this._getSalesIntegralProducts()
  // },
  _initData() {
    GetUserCRMInfo({
      data: {
        openid: app.globalData.openid
      },
      success: res => {
        console.log(res)
        if (res.code === 0) {
          this.setData({
            Name: res.data.RealName === '' ? app.globalData.userInfo.nickName : res.data.RealName,
            Level: res.data.Level,
            Integral: res.data.Integral,
            Img: res.data.image[0].URL,
            Val: res.data.val,
            isSign: res.data.isSign,
            bindPhone: res.data.phone
          })
        }
      }
    }),
    this._getQrcode()
  },
  sign() {
    daySign({
      data: {
        openid: app.globalData.openid
      },
      success: res => {
        if (res.state) {
          this._initData()
        }
        wx.showToast({
          title: res.message,
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  _getSalesIntegralProducts() {
    console.log(1111)
    this.data.pageIndex++
    gussYouLike({
      data: {
        "index": this.data.pageIndex,
        "size": 20
      },
      success: res => {
        if (res.state) {
          if (res.data.length > 0) {
            this.setData({
              productList: this.data.productList.concat(res.data)
            })
          } else {

          }
        }
      }
    })
  },
  _getQrcode() {
    getWxQrCode({
      data: {
        path: 'pages/fakeIndex/fakeIndex',
        openid: app.globalData.openid,
        from: 'cu'
      },
      success: res => {
        if (res.state) {
          this.setData({
            qrCodeImg: res.data
          })
        }
      }
    })
  },
  showQrcode() {
    this.setData({
      qrModa: true
    })
  },
  hideModal() {
    this.setData({
      qrModa: false
    })
  },
  _getBgImg() {
    getInfoPicture({
      data: {},
      success: res => {
        if (res.state) {
          this.setData({
            bgImg: res.data.background
          })
        }
      }
    })
  },
  productClick(e) {
    wx.navigateTo({
      url: '../integral-detail/integral-detail?did=' + e.currentTarget.dataset.product.id,
    })
  },
  onReachBottom: function() {
    this._getSalesIntegralProducts()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})